package com.pactera.madp.cp.controller.oms.job;

import com.pactera.madp.common.data.tenant.TenantContextHolder;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.cp.api.entity.CorpEntity;
import com.pactera.madp.cp.service.ICorpService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * WorkEmployeeSynController
 *
 * @author jun
 * @version 1.0
 * 2021/8/20 11:13
 **/
@AllArgsConstructor
@RequestMapping("/work-employee")
@RestController
@Api(value = "work-employee",tags = "成员统计定时任务")
@Slf4j
public class WorkEmployeeSynController {

    @Autowired
    private ICorpService corpService;
    @Autowired
    private IWorkEmployeeService employeeService;

    @SysLog("成员统计定时任务")
    @Inner
    @PostMapping("/syn")
    @ApiOperation(value = "成员统计定时任务")
    public void onAsyncEmpStatistic() {
        List<CorpEntity> corpEntities = corpService.list();
        for (CorpEntity entity : corpEntities) {
            TenantContextHolder.setTenantId(entity.getTenantId());
            setEmployeeStatistic(entity.getCorpId());
            log.info("定时任务执行");
        }
    }

    /**
     * 成员统计定时任务异步中转
     * @param corpId
     */
    @Async
    void setEmployeeStatistic(Integer corpId) {
        employeeService.syncEmployeeStatistic(corpId);
    }

}
